//
//  MainViewController.swift
//  MicroBlog
//
//  Created by  NorthCity on 15/11/3.
//  Copyright © 2015年  NorthCity. All rights reserved.
//
import UIKit
class MainViewController: UITabBarController {
//    MARK: - 按钮监听方法
   @objc private func clickComposeBtn(){
    let vc = UserAccountViewModel.sharedUserAccount.userLogin ? ComposeViewController() : OAuthViewController()
    let nav = UINavigationController(rootViewController: vc)
    presentViewController(nav, animated: true, completion: nil)
    }
    //MARK: - 视图声明周期函数
    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        setUpComposedButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.bringSubviewToFront(composeBtn)
    }
    // MARK: - 懒加载控件
    private lazy var composeBtn :UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
}
// MARK: - 设置界面
extension MainViewController {
    //    1 添加按钮
    private  func setUpComposedButton (){
    //    2 计算按钮的frame
        let count = childViewControllers.count
        let width = tabBar.bounds.width / CGFloat(count) - 1
        composeBtn.frame = CGRectInset(tabBar.bounds, width*2, 0)
        tabBar.addSubview(composeBtn)
//        3 添加按钮监听方法
        composeBtn.addTarget(self, action: "clickComposeBtn", forControlEvents: UIControlEvents.TouchUpInside)
    }
    //    添加所有控制器
    private func addChildViewControllers() {
        //         设置渲染颜色
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeViewController(), title: "首页", imgString: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imgString: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imgString: "tabbar_discover")
        addChildViewController( ProfileViewController(), title: "我", imgString: "tabbar_profile")
    }
    
    //    添加控制器
    private func addChildViewController(vc:UIViewController,title:String,imgString:String) {
        //        设置标题
        vc.title = title
        //        设置tabbarItem图片
        vc.tabBarItem.image = UIImage(named:imgString)
        //        添加导航控制器
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
        
    }
    
}